﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using FarseerGames.FarseerPhysics.Mathematics;
using FarseerGames.FarseerPhysics.Dynamics;

namespace Demo6
{
    public partial class SpaceShip : UserControl
    {
        //We need to keep a reference to the keyhandler, so we can act upon keys in the Update()
        private KeyHandler keyHandler;

        private TranslateTransform translate = new TranslateTransform();
        private RotateTransform rotate = new RotateTransform();

        public SpaceShip(float width, float height, float mass, KeyHandler keyHandler)
        {
            InitializeComponent();

            //Setup needed transforms - These are needed so Farseer can rotate our SpaceShip for instance
            TransformGroup transformsGroup = new TransformGroup();
            transformsGroup.Children.Add(translate);
            transformsGroup.Children.Add(rotate);
            this.RenderTransform = transformsGroup;

            //Save a reference to the keyhandler
            this.keyHandler = keyHandler;

            //Setup the Farseer Body object, which represents the SpaceShip object in the "Farseer Physics world = PhysicsSimulator"
            this.Body = SpacePhysicsHelpers.CreateRectangleBody(width, height, mass);
            this.Size = new Vector2(width, height);
        }

        public void Update()
        {

            //*******Handle input*********//
            //Rotation
            if (keyHandler.IsKeyPressed(Key.Right))
                this.Body.ApplyTorque(20000f);
            if (keyHandler.IsKeyPressed(Key.Left))
                this.Body.ApplyTorque(-20000f);

            //Force
            if (keyHandler.IsKeyPressed(Key.Up))
            {
                var force = SpaceMathHelpers.FromAngle(Body.Rotation, 10000f);
                this.Body.ApplyForce(new Vector2(force.X, -force.Y));
            }


            //*******Position and Rotate the SpaceShip based on info from the PhysicsSimulator*********//
            if (Body == null) return;
            if (Body.Position != null)
            {
                this.Position = Body.Position;
            }

            if (Body.Rotation != Rotation)
            {
                Rotation = Body.Rotation;
                RotateTransform.Angle = (Rotation * 360) / (2 * Math.PI);
            }
        }

        #region Properties
        public Body Body { get; protected set; }
        public Vector2 Position
        {
            get { return new Vector2((float)Canvas.GetLeft(this), (float)Canvas.GetTop(this)); }

            set
            {
                Canvas.SetLeft(this, Convert.ToDouble(value.X));
                Canvas.SetTop(this, Convert.ToDouble(value.Y));
            }
        }

        public Vector2 Size
        {
            set
            {
                this.Width = value.X;
                this.Height = value.Y;
                TranslateTransform.X = -this.Width / 2;
                TranslateTransform.Y = -this.Height / 2;
                //RotateTransform.CenterX = 0;//this.Width / 2;
                //RotateTransform.CenterY = 0; //this.Height / 2;
            }
            get
            {
                return new Vector2((float)this.Width, (float)this.Height);
            }
        }

        protected TranslateTransform TranslateTransform { get { return translate; } }
        protected RotateTransform RotateTransform { get { return rotate; } }
        protected float Rotation { get; set; }
        #endregion
    }
}
